package com.example.service;

import com.example.dao.WxbGoodRepository;
import com.example.model.WxbGood;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EsGoodService {
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private WxbGoodRepository wxbGoodRepository;

   public Page<WxbGood> esQueryList(String customerId, String typeId, String sortType, String alisaName,Integer pageNum,Integer size){
       NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
       BoolQueryBuilder builder = QueryBuilders.boolQuery();
       if (StringUtils.isNotBlank(customerId)){
//           term匹配，即不分词匹配，你传来什么值就会拿你传的值去做完全匹配
//           查询某个字段中模糊包含目标字符串，使用matchQuery
//           nativeSearchQueryBuilder =nativeSearchQueryBuilder.withQuery(QueryBuilders.termQuery("customerId", customerId));
           builder=builder.must(QueryBuilders.termQuery("customerId", customerId));
       }
       if (StringUtils.isNotBlank(typeId)){
//           nativeSearchQueryBuilder = nativeSearchQueryBuilder.withQuery(QueryBuilders.termQuery("typeId",typeId));
           builder=builder.must(QueryBuilders.termQuery("typeId", typeId));
       }
       if (StringUtils.isNotBlank(alisaName)){
//           multi_match多个字段匹配某字符串
//           nativeSearchQueryBuilder =nativeSearchQueryBuilder.withQuery(QueryBuilders.multiMatchQuery(alisaName, "goodName", "promoteDesc","skuTitle"));
           builder=builder.must(QueryBuilders.multiMatchQuery(alisaName, "goodName", "promoteDesc","skuTitle"));
       }
//       只能显示上架商品
       builder=builder.must(QueryBuilders.termQuery("state", 1));

       if(builder!=null){
           nativeSearchQueryBuilder= nativeSearchQueryBuilder.withQuery(builder);
       }
       if(!StringUtils.isNotBlank(sortType)){
           sortType="toped";
       }

//       if (StringUtils.isNotBlank(sortType)){
//           排序

/**             "toped"     按商品置顶排序
                "recomed"  按商品推荐排序
                "level"     按商户等级排序
 */
           SortBuilder sortBuilder = null;
           if(sortType.equals("toped")){
               sortBuilder = SortBuilders.fieldSort("toped").order(SortOrder.ASC);
           }if (sortType.equals("recomed")){
               sortBuilder = SortBuilders.fieldSort("recomed").order(SortOrder.ASC);
           }if(sortType.equals("level")){
               sortBuilder = SortBuilders.fieldSort("level").order(SortOrder.ASC);
           }

           nativeSearchQueryBuilder = nativeSearchQueryBuilder.withSort(sortBuilder);
//       }
       //由于queryForList默认分页数据是10条，重新定义分页数据，设置为每页显示5条
       if(pageNum==null||pageNum<=1){
           pageNum=1;
       }
       if (size==null){
           size=5;
       }


      Pageable pageable = PageRequest.of(pageNum-1, size);
       nativeSearchQueryBuilder=nativeSearchQueryBuilder.withPageable(pageable);
       Page<WxbGood> page = elasticsearchTemplate.queryForPage(nativeSearchQueryBuilder.build(), WxbGood.class);

       return page;
   }

   public WxbGood findGoodSku( String goodId){

       WxbGood wxbGood = wxbGoodRepository.findByGoodId(goodId);
       return wxbGood;


   }

}
